package com.css.fxfzcpxx.modules.systemOperation.service.impl;

import com.css.fxfzcpxx.base.attachment.repository.AttachmentRecordRepository;
import com.css.fxfzcpxx.base.attachment.service.AttachmentRecordService;
import com.css.fxfzcpxx.modules.categoryView.vo.CategoryViewVo;
import com.css.fxfzcpxx.modules.courseManage.repository.CourseManageRepository;
import com.css.fxfzcpxx.modules.informationAchievement.repository.InfAchievementRepository;
import com.css.fxfzcpxx.modules.informationDelivery.repository.InfDeliveryRepository;
import com.css.fxfzcpxx.modules.logRecord.repository.AccessLogRecordRepository;
import com.css.fxfzcpxx.modules.logRecord.repository.LogRecordRepository;
import com.css.fxfzcpxx.modules.systemOperation.service.SysOperationService;
import com.css.fxfzcpxx.modules.systemOperation.vo.StatisticsDataVo;
import com.css.fxfzcpxx.modules.thematicMap.repository.TheMapRepository;
import com.css.fxfzcpxx.util.PlatformObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:
 * @CreateTime: 2023/9/15 11:02
 * @Description:
 */
@Service
public class SysOperationServiceImpl implements SysOperationService {
    @Autowired
    LogRecordRepository logRecordRepository;
    @Autowired
    AccessLogRecordRepository accessLogRecordRepository;
    @Autowired
    private AttachmentRecordRepository attachmentRecordRepository;
    @Autowired
    private InfAchievementRepository infAchievementRepository;
    @Autowired
    private InfDeliveryRepository infDeliveryRepository;
    @Autowired
    private CourseManageRepository courseManageRepository;

    @Autowired
    private TheMapRepository theMapRepository;

    @Override
    public Map<String, Object> getRecordTotal() {
        Map<String, Object> map=new HashMap<>();
        //浏览量
        Integer pageView=logRecordRepository.getTotalPageView("浏览");
        //检索量
        Integer retrievalVolume=logRecordRepository.getTotalPageView("检索");
        //访问量
        Integer visits=accessLogRecordRepository.getTotalPageView();
        //下载量
        Integer download=attachmentRecordRepository.getTotalPageView();
        map.put("pageView",pageView);
        map.put("retrievalVolume",retrievalVolume);
        map.put("visits",visits);
        map.put("download",download);
        return map;
    }

    @Override
    public  List<StatisticsDataVo>  getVisitsStatisticsData(String unit) {
        return accessLogRecordRepository.getVisitsStatisticsData(unit);
    }

    @Override
    public List<StatisticsDataVo> getPageViewStatisticsData(String unit) {
        return logRecordRepository.getPageViewStatisticsData("浏览",unit);
    }

    @Override
    public List<Map<String,Object>> getRetrievalVolumeStatisticsData(String unit) {
        return logRecordRepository.getRetrievalVolumeStatisticsData("检索", unit);
    }

    @Override
    public List<StatisticsDataVo> getPageViewRankingData(String unit) {

        return logRecordRepository.getPageViewRankingData("浏览",unit);
    }

    @Override
    public List<Map<String, Object>> getDownloadStatisticsData(String unit) {
        List<Map<String, Object>> list=new ArrayList<>();
        Integer theMapDownload=theMapRepository.getDownload(unit);
        Map<String, Object> map1=new HashMap();
        map1.put("name","专题图");
        map1.put("value",theMapDownload);
        Integer infAchievementDownload=infAchievementRepository.getDownload(unit);
        Map<String, Object> map2=new HashMap();
        map2.put("name","信息成果");
        map2.put("value",infAchievementDownload);
        Integer infDeliveryDownload=infDeliveryRepository.getDownload(unit);
        Integer courseManageDownload=courseManageRepository.getDownload(unit);
        Map<String, Object> map3=new HashMap();
        map3.put("name","知识学习");
        map3.put("value",infDeliveryDownload+courseManageDownload);
        list.add(map1);
        list.add(map2);
        list.add(map3);
        return list;
    }

    @Override
    public Map<String, Object> getPageViewRankingPage(String unit, int curPage, int pageSize) {
        List<StatisticsDataVo> list = logRecordRepository.getPageViewRankingData("浏览",unit);
        List<StatisticsDataVo> pageList = startPage(list, curPage, pageSize);
        Map<String, Object> map=new HashMap<>();
        map.put("total", PlatformObjectUtils.isEmpty(list) ? 0 : list.size());
        map.put("rows", pageList);
        return map;
    }
    private  List<StatisticsDataVo> startPage(List<StatisticsDataVo> list, Integer pageNum, Integer pageSize) {
        if (list.size() == 0) {
            return list;
        }
        Integer count = list.size(); // 记录总数
        Integer pageCount = 0; // 页数
        if (count % pageSize == 0) {
            pageCount = count / pageSize;
        } else {
            pageCount = count / pageSize + 1;
        }
        int fromIndex = 0; // 开始索引
        int toIndex = 0; // 结束索引
        if (pageNum != pageCount) {
            fromIndex = (pageNum - 1) * pageSize;
            toIndex = fromIndex + pageSize;
        } else {
            fromIndex = (pageNum - 1) * pageSize;
            toIndex = count;
        }
        List<StatisticsDataVo> pageList = list.subList(fromIndex, toIndex);
        return pageList;
    }
}
